package com.ryan.rss.war.activity;

import java.io.InputStream;

import java.util.List;

import cn.guomob.android.GuomobAdView;
import cn.guomob.android.OnBannerAdListener;

import com.ryan.rss.war.adapter.ArticleAdapter;
import com.ryan.rss.war.common.XmlParseUtil;
import com.ryan.rss.war.database.DBManager;

import com.ryan.rss.war.ui.fragment.ArticleFragment;
import com.ryan.rss.war.util.Globals;
import com.ryan.rss.war.util.Network;
import com.ryan.rss.war.util.RSSItem;
import com.ryan.rss.war.R;

import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengUpdateAgent;

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.content.SharedPreferences;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.RelativeLayout;

/**
 * @author Ryan
 * 
 *         2013-4-7
 */
public class ArticleActivity extends FragmentActivity {

	private final static String LOGTAG = "MainActivity";
	private final static boolean DEBUG_MODE = false;

	private DBManager dbManager; // 数据库管理对象
	private List<RSSItem> rssList;// http下载rss xml后解析出来的数据,写入数据库
	SharedPreferences setting;

	/* 使用广告 */

	GuomobAdView m_adView;
	RelativeLayout m_Relative;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		UmengUpdateAgent.update(this);
		Log.v(LOGTAG, "onCreate");

		setContentView(R.layout.activity_base);

		Globals.NETWORK_ENABLE = Network.isConnect(this);// 判断网络

		if (Globals.AD_MODE && Globals.NETWORK_ENABLE) {
			// 有网络的情况下才加载广告
			// initADView();
		}

		readPreference();

		// 初始化数据库管理对象
		// 使用sdcard
		dbManager = DBManager.get();
		dbManager.init(ArticleActivity.this, Globals.DATABASE_NAME,
				Globals.DATABASE_PATH);

		// dbManager = new DBManager(ArticleActivity.this);
		if (Globals.prefs_isFirstRun) {

			updateLocalContent();
			// 第一次运行时更新数据
			// updateListContent();
			Globals.prefs_isFirstRun = false;
		}

		getSupportFragmentManager().beginTransaction()
				.replace(R.id.activity_base_content, new ArticleFragment())
				.commit();

	}

	public void updateLocalContent() {
		try {
			InputStream is = getAssets().open("data/war3.xml");
			rssList = XmlParseUtil.getRssDate(is);

			dbManager.openDataBase();
			dbManager.deleteAll();
			dbManager.intert(rssList);// 写入到数据库中
			dbManager.closeDataBase();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void onPause() {
		// ument
		MobclickAgent.onPause(this);
		Log.v(LOGTAG, "onPause()");
		super.onPause();

	}

	@Override
	protected void onResume() {
		// ument
		MobclickAgent.onResume(this);
		Log.v(LOGTAG, "onResume()");
		super.onResume();

	}

	@Override
	protected void onDestroy() {

		writePreference();
		Log.v(LOGTAG, "onDestroy()");
		super.onDestroy();

	}

	@Override
	protected void onRestart() {

		Log.v(LOGTAG, "onRestart()");

		super.onRestart();
	}

	@Override
	protected void onStart() {

		Log.v(LOGTAG, "onStart()");

		super.onStart();
	}

	@Override
	protected void onStop() {

		Log.v(LOGTAG, "onStop()");

		super.onStop();

	}

	/**
	 * 读取设置
	 */
	private void readPreference() {

		setting = this.getSharedPreferences("setting", 0);

		Globals.prefs_isFirstRun = setting.getBoolean("isfirstrunv31",
				Globals.prefs_isFirstRun);
		// Globals.prefs_autoScroll = setting.getBoolean("autoscroll",
		// Globals.prefs_autoScroll);
		// Globals.prefs_categoryIndex = setting.getInt("category",
		// Globals.prefs_categoryIndex);
		// Globals.prefs_recent_link = setting.getString("link",
		// Globals.prefs_recent_link);
		// Globals.prefs_textSize = setting.getInt("textsize",
		// Globals.prefs_textSize);
		// Globals.prefs_scrollSpeed = setting.getInt("scrollspeed",
		// Globals.prefs_scrollSpeed);
		// Globals.prefs_last_modify_date = setting.getString("lastmodify",
		// Globals.prefs_last_modify_date);
	}

	/**
	 * 写入设置
	 */
	private void writePreference() {

		SharedPreferences.Editor editor = setting.edit();

		editor.putBoolean("isfirstrunv31", Globals.prefs_isFirstRun);
		// editor.putBoolean("autoscroll", Globals.prefs_autoScroll);
		// editor.putInt("category", Globals.prefs_categoryIndex);
		// editor.putString("link", Globals.prefs_recent_link);
		// editor.putInt("textsize", Globals.prefs_textSize);
		// editor.putInt("scrollspeed", Globals.prefs_scrollSpeed);
		// editor.putString("lastmodify", Globals.prefs_last_modify_date);
		// editor.commit();

	}

}
